<cfcomponent>
	
	<!--- 	Date: 11/9/2007  Usage: I perform initialization functions for this component --->
	<cffunction name="init" output="false" access="public" returntype="KalendarEventValidator" hint="I perform initialization functions for this component">
		<cfreturn this />		
	</cffunction>

	<!--- validate --->
	<cffunction name="validate" access="public" output="false">
		<cfargument name="KalendarEvent" required="true" />
		<cfargument name="UserMSG" required="true" />

		<cfset var ResultStruct = StructNew() />
		<cfset var isValid = false />
		
		<!--- ProcessRules --->
		
		<cfset ResultStruct[ validateCategory( ArgumentCollection=arguments) ] = "" />
		<cfset ResultStruct[ validateStartDate( ArgumentCollection=arguments) ] = "" />
		<cfset ResultStruct[ validateStartAndEndDate( ArgumentCollection=arguments) ] = "" />
		<cfset ResultStruct[ validateDescription( ArgumentCollection=arguments) ] = "" />
		
		<!--- See if we passed validation --->
		<cfif NOT structKeyExists( ResultStruct, "False")>
			<cfset isValid = true>
		</cfif>
		
		<cfreturn isValid />
	
	</cffunction>
	
	<cffunction name="validateCategory" access="public" output="false">
		<cfargument name="KalendarEvent" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />

		<!--- Category is required. Either pass one, or add a new one --->
		<cfif not len( trim( arguments.KalendarEvent.getCategoryID() & arguments.KalendarEvent.getNewCategoryName() ) )>
			<cfset Error="The Category for this event is mandatory. " />
		
		<!--- Category Names may be 50 characters max --->
		<cfelseif len( arguments.KalendarEvent.getNewCategoryName() ) GT 50>
			<cfset Error="Please reduce the length of your New Category to 50 characters or less" />
		</cfif>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>
	
	<cffunction name="validateStartDate" access="public" output="true">
		<cfargument name="KalendarEvent" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />
		<cftry>
			<cfset CreateODBCDateTime( arguments.KalendarEvent.getStartDate() )/>
			<cfcatch type="any">
				<cfset Error = "Please verify the Start Date is accurate." />
				<cfset arguments.KalendarEvent.dump()/>
				<cfdump var="#cfcatch#"><cfabort>
			</cfcatch>
		</cftry>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>	
		
	<cffunction name="validateStartAndEndDate" access="public" output="false">
		<cfargument name="KalendarEvent" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />
		<cfset var startDate = ""/>
		<cfset var endDate = ""/>
		<cftry>
			<cfset startDate = CreateODBCDAteTime( arguments.KalendarEvent.getStartDate() )/>
			<cfset endDate = CreateODBCDAteTime( arguments.KalendarEvent.getEndDate() )/>
			<cfif DateCompare( startDate, endDate ) GT 0>			
				<cfset Error = "Please verify the Start Date is before the End Date" />
			</cfif>
			<cfcatch type="any">
				<cfset Error = "Please verify the Start Date is before the End Date" />
			</cfcatch>
		</cftry>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>	
	
	<cffunction name="validateDescription" access="public" output="false">
		<cfargument name="KalendarEvent" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />

		<!--- Description is required--->
		<cfif NOT len( trim( arguments.KalendarEvent.getDescription() ) ) >			
			<cfset Error = "Please enter a description for your event" />
		</cfif>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>	
	
	<cffunction name="processError" output="false" access="private" returntype="boolean" hint="I add errors to the user defined collection">
		<cfargument name="MsgCollection" type="any" required="true"/>
		<cfargument name="Message" type="string" default="" />
		<cfset var isOK = true />

			<cfif len( trim( arguments.Message ) )>
				<cfset isOK = false />		
				<cfset arguments.MsgCollection.addError( arguments.Message ) />
			</cfif>

		<cfreturn isOK />
		
	</cffunction>
	


</cfcomponent>